package com.zzptc.myk.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zzptc.myk.myapplication.Bean.User;
import com.zzptc.myk.myapplication.Fragment.FirsFragment;
import com.zzptc.myk.myapplication.Fragment.SecondFragment;
import com.zzptc.myk.myapplication.Fragment.ThidFragment;

import org.litepal.LitePal;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawerLayou;
    private LinearLayout main_left;
    private TextView menutxt1,menutxt2,menutxt3,menutxt4,menuuserid,menuusername;
    private Button tofirst,tosecond,tothird;
    private FirsFragment firsFragment=new FirsFragment();
    private SecondFragment secondFragment=new SecondFragment();
    private ThidFragment thirdFragment=new ThidFragment();
    String btnBackColor="#FFBBFF";
    String btnChooseColor="#FF82AB";
    String menuBackClolor="#FFDEAD";
    String menuChooseColor="#87CEEB";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        replaceFragment(firsFragment);
        gerUser();
        initView();
        setListener();
    }

    private void gerUser() {
        List<User> userList= LitePal.findAll(User.class);
        if (userList.size()==0){
            User user=new User(0,"张三","123");
            User user1=new User(0,"李四","456");
            User user2=new User(0,"王五","789");
            user.save();
            user1.save();
            user2.save();
        }
    }

    private void setListener() {
        View.OnClickListener listener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch ( v.getId()){
                    case R.id.tofirst:
                    case R.id.menutxt1:
                        initColor();
                        tofirst.setBackgroundColor(Color.parseColor(btnChooseColor));
                        menutxt1.setBackgroundColor(Color.parseColor(menuChooseColor));
                        replaceFragment(firsFragment);
                        break;
                    case R.id.tosecond:
                    case R.id.menutxt2:
                        initColor();
                        tosecond.setBackgroundColor(Color.parseColor(btnChooseColor));
                        menutxt2.setBackgroundColor(Color.parseColor(menuChooseColor));
                        replaceFragment(secondFragment);
                        break;
                    case R.id.tothird:
                    case R.id.menutxt3:
                        initColor();
                        tothird.setBackgroundColor(Color.parseColor(btnChooseColor));
                        menutxt3.setBackgroundColor(Color.parseColor(menuChooseColor));
                        replaceFragment(thirdFragment);
                        break;
                    case R.id.menutxt4:
                        menutxt4.setBackgroundColor(Color.parseColor(menuChooseColor));
                        Intent intent=new Intent(MainActivity.this,LoginActivity.class);
                        startActivity(intent);
                        finish();
                        break;
                }
               drawerLayou.closeDrawer(main_left);
            }
        };
        tofirst.setOnClickListener(listener);
        tosecond.setOnClickListener(listener);
        tothird.setOnClickListener(listener);
        menutxt1.setOnClickListener(listener);
        menutxt2.setOnClickListener(listener);
        menutxt3.setOnClickListener(listener);
        menutxt4.setOnClickListener(listener);
    }

    private void initView() {
        drawerLayou=findViewById(R.id.drawerlayout);
        main_left=findViewById(R.id.main_left);
        menutxt1=findViewById(R.id.menutxt1);
        menutxt2=findViewById(R.id.menutxt2);
        menutxt3=findViewById(R.id.menutxt3);
        menutxt4=findViewById(R.id.menutxt4);
        menuuserid=findViewById(R.id.menuuserid);
        menuusername=findViewById(R.id.menuusername);
        tofirst=findViewById(R.id.tofirst);
        tosecond=findViewById(R.id.tosecond);
        tothird=findViewById(R.id.tothird);
        initColor();
        tofirst.setBackgroundColor(Color.parseColor(btnChooseColor));
        menutxt1.setBackgroundColor(Color.parseColor(menuChooseColor));
    }

    private void initColor(){
        tofirst.setBackgroundColor(Color.parseColor(btnBackColor));
        tosecond.setBackgroundColor(Color.parseColor(btnBackColor));
        tothird.setBackgroundColor(Color.parseColor(btnBackColor));
        menutxt1.setBackgroundColor(Color.parseColor(menuBackClolor));
        menutxt2.setBackgroundColor(Color.parseColor(menuBackClolor));
        menutxt3.setBackgroundColor(Color.parseColor(menuBackClolor));
        menutxt4.setBackgroundColor(Color.parseColor(menuBackClolor));
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container,fragment);
        transaction.commit();

    }
}
